<?php

class MemberPayoutValue extends DataExtension implements PermissionProvider {
	private static $db = array(
		'MaxPayout' => 'Currency',
		'TotalPayout' => 'Currency'
	);
	
	private static $casting = array(
		'IsExceedPayout' => 'Boolean'
	);
	
	function updateFieldLabels(&$labels) {
        $labels['MaxPayout'] = _t('MemberPayoutValue.MAX_PAYOUT', 'Max Payout');
		$labels['TotalPayout'] = _t('MemberPayoutValue.TOTAL_PAYOUT', 'Total Payout');
	}
	
	function updateCMSFields(FieldList $fields) {
		if(!Permission::check('ALLOW_PayoutValue')){
			$fields->removeByName('MaxPayout');
        	$fields->removeByName('TotalPayout');
		}
		else{
	        if($this->owner->exists()) {
	            $fields->makeFieldReadonly('TotalPayout');
	        } else {
	        	$fields->removeByName('TotalPayout');
	        }
		}
    }

	function updateFrontendFields(FieldList $fields) {
		$fields->removeByName('MaxPayout');
        $fields->removeByName('TotalPayout');
    }

	function updateMemberFormFields(FieldList $fields) {
        $fields->removeByName('MaxPayout');
        $fields->removeByName('TotalPayout');
    }
	
	function getIsExceedPayout(){
		return ($this->owner->TotalPayout >= $this->owner->MaxPayout);
	}
	
	public function providePermissions() {
        return array(
            'ALLOW_PayoutValue' => array(
                'name' => _t('MemberPayoutValue.PERMISSION_ALLOW_PAYOUT_VALUE', 'Allow edit payout value'),
                'category' => ''
            )
        );
    }
}
